﻿using Baidu.Api;
using Baidu.Api.Request;
using Baidu.Api.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

namespace BaiduTestForm
{
    public static class PCSHelper
    {
        public static string APP_Path = ConfigurationManager.AppSettings["AppPath"];
        public const int bufferSize = 256 * 1024;

        /// <summary>
        /// 上传单个文件
        /// </summary>
        public static bool UploadSingleFile(this IBaiduClient client, string path)
        {
            return UploadSingleFile(client, path, APP_Path);
        }

        public static bool UploadSingleFile(this IBaiduClient client, string path, string appPath)
        {
            var req = new FileUploadRequest();
            req.File = new FileItem(path);
            req.Path = PathCombine(appPath, Path.GetFileName(path));
            req.Ondup = "newcopy";
            var rsp = client.Execute("https://c.pcs.baidu.com/rest/2.0/", req);
            return rsp.IsError;
        }

        /// <summary>
        /// 秒传文件
        /// </summary>
        public static bool CloudMatch(this IBaiduClient client, string path)
        {
            return CloudMatch(client, path, APP_Path);
        }

        public static bool CloudMatch(this IBaiduClient client, string path, string appPath)
        {
            var req = new FileRapidUploadRequest();

            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                if (fs.Length < BaiduUtils.SLICE_SIZE) return false;

                req.ContentLength = (uint)fs.Length;
                req.ContentMD5 = BaiduUtils.GetMD5(fs);
                req.SliceMD5 = BaiduUtils.GetSliceMD5(fs);
                req.ContentCrc32 = BaiduUtils.GetCrc32(fs);

                fs.Close();
            }

            req.Path = PathCombine(appPath, Path.GetFileName(path));
            req.Ondup = "newcopy";
            var rsp = client.Execute(req);
            return !rsp.IsError;
        }

        public static bool MergeFile(this IBaiduClient client, string[] blocklist, string fileName)
        {
            return MergeFile(client, blocklist, fileName, APP_Path);
        }

        public static bool MergeFile(this IBaiduClient client, string[] blocklist, string fileName, string path)
        {
            var req = new SuperFileCreateRequest();
            req.Path = PathCombine(path, fileName);
            req.Param = blocklist;
            req.Ondup = "newcopy";
            var rsp = client.Execute(req);
            return rsp.IsError;
        }

        public static IEnumerable<byte[]> GetBytes(string filepath, int bufferSize = bufferSize)
        {
            int read;
            byte[] buffer = new byte[bufferSize];

            using (var fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
            {
                while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    yield return buffer.Take(read).ToArray();
                }
                fs.Close();
            }
        }

        public static string PathCombine(params string[] paths)
        {
            return Path.Combine(paths).Replace('\\', '/');
        }
    }
}
